<template>
  <el-dialog :visible.sync="visible" :title="title" width="600px" :close-on-click-modal="false">
    <VForm :fieldList="fieldList" label-width="90px" ref="form" v-model="formData"></VForm>
    <div slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="$wait.is('account.role.*Role')">保 存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import VForm from '@/components/VForm';
import request from '../../api/http';
import qa from './qaFormDialog';
import issue from './issueFormDialog';
import progress from './progressFormDialog';

export default {
  name: 'qaFormDialog',
  mixins: [qa, issue, progress],
  components: {
    VForm,
  },
  props: {
    formType: {
      type: String,
      default: 'qa',
    },
  },
  data() {
    return {
      visible: false,
      type: 'add',
      formData: {},
    };
  },

  computed: {
    title() {
      const { type } = this;
      return `${type === 'edit' ? '编辑' : '新增'}`;
    },
    fieldList() {
      return this[this.formType];
    },
  },
  methods: {
    // 获取产品
    getMateriel() {
      const opt = [];
      this.$api.ad_materiel
        .getList({
          page: 1,
          pageSize: 100000,
        })
        .then(({ data }) => {
          data.forEach(child => {
            opt.push({
              value: child.id,
              label: child.num,
            });
          });
        });
      return opt;
    },
    // 获取装配工
    getUser() {
      const opt = [];
      request.get('/user/api/list?page=1&pageSize=1000000').then(res => {
        res.data.data.forEach(child => {
          opt.push({
            value: child.id,
            label: child.name,
          });
        });
      });
      return opt;
    },
    show(type = 'add', model) {
      this.visible = true;

      this.type = type;

      this.$nextTick(() => {
        this.reset();
        if (this.type === 'edit') {
          this.formData = {
            ...model,
          };
        }
      });
    },

    close() {
      this.visible = false;
    },

    reset() {
      this.formData = {};
      this.$refs.form && this.$refs.form.resetFields();
    },

    async handleSubmit() {
      const { type } = this;
      let params = await this.$refs.form.submit();

      await this.$api.aa_seePlate[`${this.formType}Create`](params);
      this.$message.success('保存成功！');
      this.close();

      this.$emit('submit', this.formType);
    },
  },
};
</script>

<style lang="scss" scoped></style>
